package com.accounting.client.frontend.common;

import com.accounting.client.frontend.customer.dialog.AddCustomerHandler;
import com.accounting.client.remote.Services;
import com.accounting.client.remote.enumeration.Sex;
import com.accounting.client.remote.obj.Customer;
import com.accounting.client.resource.BoxResources;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.Util;
import corelib.client.delegate.Action;
import corelib.client.delegate.DataCallback;
import corelib.client.resource.CommonResources;
import corelib.client.schedule.DeferredTask;
import corelib.client.search.PagingRequest;
import corelib.client.search.PagingResult;
import corelib.client.ui.SimpleMask;
import corelib.client.ui.widget.ImageProvider;
import corelib.client.ui.widget.Size;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.WindowHandle;
import corelib.client.ui.widget.button.Button;
import corelib.client.ui.widget.button.IsButton;
import corelib.client.ui.widget.dialog.SimpleDialog;
import corelib.client.ui.widget.dialog.SimpleDialogHandler;
import corelib.client.ui.widget.form.SearchBox;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.panel.SimpleLayoutPanel;
import corelib.client.ui.widget.panel.SimpleListPanel;
import corelib.client.ui.widget.toolbar.SimpleRibbon;

public class CustomerSelector extends SimpleDialogHandler {
	
	private SimpleRibbon toolBar;
	private SearchBox box;
	private SimpleListPanel<Customer> listPanel;
	private DataCallback<Customer> callback;
	
	public CustomerSelector(DataCallback<Customer> callback){
		this.callback = callback;
	}
	
	@Override
	public String getHeading() {
		return "选择收件人";
	}

	@Override
	public Size getSize() {
		return new Size(600, 600);
	}

	@Override
	public Widget getContentPanel() {
		toolBar   = createToolBar();
		listPanel = createContentPanel();
		SimpleLayoutPanel layout = new SimpleLayoutPanel();
		layout.add(toolBar);
		layout.add(listPanel);
		layout.setWidgetTopHeight(toolBar, 0, Unit.PX, 32, Unit.PX);
		layout.setWidgetTopBottom(listPanel, 44, Unit.PX, 0, Unit.PX);
		return layout;
	}
	
	protected SimpleRibbon createToolBar(){
		SimpleRibbon toolBar = new SimpleRibbon();
		toolBar.getElement().getStyle().setPaddingTop(15, Unit.PX);
		box = new SearchBox(new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				new DeferredTask().run(new ScheduledCommand(){
					@Override
					public void execute() {
						new DeferredTask().run(new ScheduledCommand(){
							@Override
							public void execute() {
								refresh();
							}
						},500);
					}
				}, 1000);
			}
		});
		box.setEmptyText("eg. 姓名或者电子邮件");
		box.setWidth(350);
		
		Button addCustomer = new Button("增加收件人", BoxResources.INSTANCE.add16(), new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				SimpleDialog.show(new AddCustomerHandler(new Action() {
					@Override
					public void run() {
						refresh();
					}
				}));
			}
		});
		addCustomer.setWidth(120);
		addCustomer.getElement().getStyle().setMarginLeft(5, Unit.PX);
		toolBar.addWidget(box);
		toolBar.addWidget(addCustomer);
		return toolBar;
	}
	
	protected SimpleListPanel<Customer> createContentPanel() {
		listPanel = new SimpleListPanel<Customer>(new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				Customer info = listPanel.getSelectedOption();
				if (info != null) {
					
				}
			}
		});
		listPanel.setOptionFormatter(new ValueFormatter<Customer>(){
			@Override
			public String format(Customer value) {
				return value.getCName() + " " + Util.getString(value.getAddress1());
			}
		});
		
		listPanel.setIconProvider(new ImageProvider<Customer>(){
			@Override
			public AbstractImagePrototype getImage(Customer value) {
				if (value.getSex() == Sex.Female){
					return AbstractImagePrototype.create(BoxResources.INSTANCE.userFemale16());
				}
				return AbstractImagePrototype.create(BoxResources.INSTANCE.user16());
			}
		});
		return listPanel;
	}
	
	@Override
	public IsButton[] getButtons() {
		return new IsButton[] {
			   new IsButton("完成", CommonResources.INSTANCE.accept16(), 100, new WidgetListener(){
					@Override
					public void valueUpdated(Widget widget) {
						if (callback != null) {
							Customer customerInfo = listPanel.getSelectedOption();
							if (customerInfo != null){
								callback.onCompleted(customerInfo);
								closeDialog();
							}
						}
					}
			   }),
			   new IsButton("关闭", BoxResources.INSTANCE.cancel16(), 100, new WidgetListener(){
					@Override
					public void valueUpdated(Widget widget) {
						closeDialog();
					}
			   })
		};
	}
	
	@Override
	public boolean isCloseable() {
		return false;
	}

	@Override
	public void onClosing(WindowHandle windowHandle) {
		windowHandle.close();
	}
	
	@Override
	public void onShowing() {
		new DeferredTask().run(new ScheduledCommand(){
			@Override
			public void execute() {
				refresh();
			}
		},1000);
	}
	
	private void refresh() {
		String passport = SessionManager.getPassport();
		PagingRequest request = new PagingRequest();
		request.setKeyword(box.getTrimmedText());
		request.setOffset(0);
		request.setLimit(1000);
		SimpleMask.show(listPanel,"努力加载中...");
		Services.CustomerService.ListCustomers(passport, request, 
			new AsyncCallback<PagingResult<Customer>>() {
				@Override
				public void onFailure(Throwable caught) {
					SimpleMask.hide(listPanel);
					ErrorHandler.handleServiceError(caught);
				}
				
				@Override
				public void onSuccess(PagingResult<Customer> result) {
					SimpleMask.hide(listPanel);
					if (listPanel != null) {
						listPanel.setOptions(result.getData());
					}
				}
			}
		);
	}

}
